<?php
header("Pragma: public");
header("Expires: 0");
header("Accept-Ranges: bytes");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment; filename=expelledstudents.xls');
header("Content-Transfer-Encoding: binary");
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="art-article">
  <tr>
    <th width="40%">ФИО</th>
    <th width="10%">Дата рождения</th>
    <th width="40%">Адрес места жительства</th>
    <th width="10%">Дата и номер приказа об отчислении</th>
  </tr>
 <?php 
 foreach ($students as $student):
 
 ?> 
  <tr>
    <td width="40%">
		<?php 
			echo $student['Student']['lastname'].' ';
			echo $student['Student']['firstname'].' ';
			echo $student['Student']['surname'];
		?>
	</td>
    <td width="10%">
		<?php 
			echo $student['Studentinfo'][0]['birthdate'];
		?>
	</td>
    <td width="40%">
		<?php 
			echo $student['Studentinfo'][0]['passport_adress'];
		?>
	</td>
    <td width="10%">
		<?php 
			$orders = $student['Order'];
			$order_date = '';
			$order_number = '';
			$order_info = '';
			foreach ($orders as $order):
				if (in_array($order['ordertype_id'], $ordertype_ids)) {
					if (!$order_date) {
						$order_date = $order['date'];
						$order_number = $order['number'];
						$order_info = $order['order_info'];
					} else if ($order['date'] > $order_date) {
						$order_date = $order['date'];
						$order_number = $order['number'];	
						$order_info = $order['order_info'];
					}
					
				}
			endforeach;
			echo $order_number.' ';
			echo $order_date.' ';
			echo $order_info;
		?>
	</td>
  </tr>
 <?php 
 endforeach;
 
 ?> 
</table>